#include "Point.h"
#include <iostream>
#include <QtCore/qmath.h>
#include <QDebug>

Point::Point(double x, double y, double z) :
    m_x(x), m_y(y), m_z(z)
{
}

Point::~Point()
{

}

void Point::set(double x, double y, double z)
{
    m_x = x;
    m_y = y;
    m_z = z;
}

void Point::setX(double x)
{
    m_x = x;
}

void Point::setY(double y)
{
    m_y = y;
}

void Point::setZ(double z)
{
    m_z = z;
}

double Point::x() const
{
    return m_x;
}

double Point::y() const
{
    return m_y;
}

double Point::z() const
{
    return m_z;
}

Point Point::normalized()
{
    if (magnitude() > 0.) {
        return (*this) / magnitude();
    } else {
        return (*this);
    }
}

double Point::magnitude()
{
    return qSqrt(x() * x() + y() * y() + z() * z());
}

void Point::limit(double max)
{
    double lengthSquared = x() * x() + y() * y() + z() * z();

    if ((lengthSquared > max * max) && (lengthSquared > 0))
    {
        double ratio = max / qSqrt(lengthSquared);
        set(x() * ratio, y() * ratio, z() * ratio);
    }
}

Point& Point::operator=(const Point& p)
{
    set(p.x(), p.y(), p.z());
    return *this;
}

Point Point::operator+(const Point p) const
{
    return Point(x() + p.x(), y() + p.y(), z() + p.z());
}

Point Point::operator-(const Point p) const
{
    return Point(x() - p.x(), y() - p.y(), z() - p.z());
}

Point Point::operator*(const Point p) const
{
    return Point(x() * p.x(), y() * p.y(), z() * p.z());
}

Point Point::operator*(double v) const
{
    return Point(x() * v, y() * v, z() * v);
}

Point Point::operator/(const Point p) const
{
    Point point = *this;
    if (p.x() != 0)
    {
        point.setX(point.x() / p.x());
    }
    if (p.y() != 0)
    {
        point.setY(point.y() / p.y());
    }
    if (p.z() != 0)
    {
        point.setZ(point.z() / p.z());
    }
    return point;
}

Point Point::operator/(double v) const
{
    if (v == 0.)
    {
        return *this;
    }
    else
    {
        return Point(x() / v, y() / v, z() / v);
    }
}

//For debugging
void Point::debugOut()
{
    qDebug() << "x=" << x() << " y=" << y() << " z=" << z() << "\n";
}
